#[cfg(test)]
mod tests {
    use super::super::ai_assistant::*;
    use cdb_ai::GeneratedSql;

    #[test]
    fn test_ai_assistant_creation() {
        let assistant = AiAssistant::new();
        assert_eq!(assistant.current_input(), "");
        assert!(!assistant.is_generating());
        assert_eq!(assistant.generated_queries().len(), 0);
    }

    #[test]
    fn test_ai_assistant_input_change() {
        let mut assistant = AiAssistant::new();
        let result = assistant.update(AiAssistantMessage::InputChanged("test query".to_string()));
        
        assert_eq!(assistant.current_input(), "test query");
        assert!(result.is_none());
    }

    #[test]
    fn test_ai_assistant_generate_sql() {
        let mut assistant = AiAssistant::new();
        
        // First set some input
        assistant.update(AiAssistantMessage::InputChanged("find all users".to_string()));
        
        // Then try to generate SQL
        let result = assistant.update(AiAssistantMessage::GenerateSql);
        
        assert!(result.is_some());
        assert_eq!(result.unwrap(), "find all users");
        assert!(assistant.is_generating());
    }

    #[test]
    fn test_ai_assistant_generate_sql_empty_input() {
        let mut assistant = AiAssistant::new();
        
        // Try to generate SQL with empty input
        let result = assistant.update(AiAssistantMessage::GenerateSql);
        
        assert!(result.is_none());
        assert!(!assistant.is_generating());
    }

    #[test]
    fn test_ai_assistant_clear_input() {
        let mut assistant = AiAssistant::new();
        
        // Set some input first
        assistant.update(AiAssistantMessage::InputChanged("test query".to_string()));
        assert_eq!(assistant.current_input(), "test query");
        
        // Clear input
        let result = assistant.update(AiAssistantMessage::ClearInput);
        
        assert_eq!(assistant.current_input(), "");
        assert!(result.is_none());
    }

    #[test]
    fn test_ai_assistant_apply_sql() {
        let mut assistant = AiAssistant::new();
        let sql = "SELECT * FROM users;".to_string();
        
        let result = assistant.update(AiAssistantMessage::ApplySql(sql.clone()));
        
        assert!(result.is_some());
        assert_eq!(result.unwrap(), sql);
    }

    #[test]
    fn test_ai_assistant_sql_generated_success() {
        let mut assistant = AiAssistant::new();
        
        // First start generating
        assistant.update(AiAssistantMessage::InputChanged("find users".to_string()));
        assistant.update(AiAssistantMessage::GenerateSql);
        assert!(assistant.is_generating());
        
        // Then receive successful result
        let generated_sql = GeneratedSql {
            sql: "SELECT * FROM users;".to_string(),
            explanation: "This query finds all users".to_string(),
            confidence: 0.9,
            warnings: vec![],
            suggested_tables: vec!["users".to_string()],
            query_type: cdb_ai::sql_gen::QueryType::Select,
        };
        
        let result = assistant.update(AiAssistantMessage::SqlGenerated(Ok(generated_sql.clone())));
        
        assert!(!assistant.is_generating());
        assert_eq!(assistant.generated_queries().len(), 1);
        assert_eq!(assistant.generated_queries()[0].sql, generated_sql.sql);
        assert!(result.is_none());
    }

    #[test]
    fn test_ai_assistant_sql_generated_error() {
        let mut assistant = AiAssistant::new();
        
        // First start generating
        assistant.update(AiAssistantMessage::InputChanged("invalid query".to_string()));
        assistant.update(AiAssistantMessage::GenerateSql);
        assert!(assistant.is_generating());
        
        // Then receive error result
        let error_message = "Failed to generate SQL".to_string();
        let result = assistant.update(AiAssistantMessage::SqlGenerated(Err(error_message)));
        
        assert!(!assistant.is_generating());
        assert_eq!(assistant.generated_queries().len(), 0);
        assert!(result.is_none());
    }

    #[test]
    fn test_ai_assistant_history_limit() {
        let mut assistant = AiAssistant::new();
        
        // Generate more than 10 queries to test limit
        for i in 0..15 {
            let generated_sql = GeneratedSql {
                sql: format!("SELECT * FROM table_{};", i),
                explanation: format!("Query {}", i),
                confidence: 0.8,
                warnings: vec![],
                suggested_tables: vec![format!("table_{}", i)],
                query_type: cdb_ai::sql_gen::QueryType::Select,
            };
            
            assistant.update(AiAssistantMessage::SqlGenerated(Ok(generated_sql)));
        }
        
        // Should be limited to 10 queries
        assert_eq!(assistant.generated_queries().len(), 10);
        
        // Should keep the most recent ones (0-9, with 0 being most recent)
        assert!(assistant.generated_queries()[0].sql.contains("table_14"));
        assert!(assistant.generated_queries()[9].sql.contains("table_5"));
    }

    #[test]
    fn test_ai_assistant_message_clone() {
        let message1 = AiAssistantMessage::InputChanged("test".to_string());
        let message2 = message1.clone();
        
        match (message1, message2) {
            (AiAssistantMessage::InputChanged(s1), AiAssistantMessage::InputChanged(s2)) => {
                assert_eq!(s1, s2);
            }
            _ => panic!("Messages should be the same"),
        }
    }

    #[test]
    fn test_ai_assistant_message_debug() {
        let message = AiAssistantMessage::GenerateSql;
        let debug_str = format!("{:?}", message);
        assert!(debug_str.contains("GenerateSql"));
    }

    #[test]
    fn test_ai_assistant_default() {
        let assistant1 = AiAssistant::new();
        let assistant2 = AiAssistant::default();
        
        assert_eq!(assistant1.current_input(), assistant2.current_input());
        assert_eq!(assistant1.is_generating(), assistant2.is_generating());
        assert_eq!(assistant1.generated_queries().len(), assistant2.generated_queries().len());
    }

    #[test]
    fn test_ai_assistant_complex_workflow() {
        let mut assistant = AiAssistant::new();
        
        // 1. Input a query
        assistant.update(AiAssistantMessage::InputChanged("find all active users".to_string()));
        assert_eq!(assistant.current_input(), "find all active users");
        
        // 2. Generate SQL
        let result = assistant.update(AiAssistantMessage::GenerateSql);
        assert!(result.is_some());
        assert!(assistant.is_generating());
        
        // 3. Receive successful result
        let generated_sql = GeneratedSql {
            sql: "SELECT * FROM users WHERE status = 'active';".to_string(),
            explanation: "This query finds all active users".to_string(),
            confidence: 0.95,
            warnings: vec!["Consider adding LIMIT clause".to_string()],
            suggested_tables: vec!["users".to_string()],
            query_type: cdb_ai::sql_gen::QueryType::Select,
        };
        
        assistant.update(AiAssistantMessage::SqlGenerated(Ok(generated_sql.clone())));
        assert!(!assistant.is_generating());
        assert_eq!(assistant.generated_queries().len(), 1);
        
        // 4. Apply the generated SQL
        let apply_result = assistant.update(AiAssistantMessage::ApplySql(generated_sql.sql.clone()));
        assert!(apply_result.is_some());
        assert_eq!(apply_result.unwrap(), generated_sql.sql);
        
        // 5. Clear input for next query
        assistant.update(AiAssistantMessage::ClearInput);
        assert_eq!(assistant.current_input(), "");
        
        // History should still be preserved
        assert_eq!(assistant.generated_queries().len(), 1);
    }
}
